package org.groupg.practice;

import org.mapdb.*;
import java.util.Map;
import java.util.stream.Collectors;

public class IterationAndQueryExample {
    public static void main(String[] args) {
        DB db = DBMaker.memoryDB().make();
        Map<Integer, String> map = (Map<Integer, String>) db.hashMap("data").create();

        // 添加数据
        map.put(1, "Apple");
        map.put(2, "Banana");
        map.put(3, "Cherry");
        map.put(4, "Date");
        map.put(5, "Elderberry");

        // 方法1: 使用forEach
        System.out.println("All entries:");
        map.forEach((key, value) -> System.out.println(key + ": " + value));

        // 方法2: 使用流处理
        System.out.println("\nEntries with key > 3:");
        map.entrySet().stream()
            .filter(entry -> entry.getKey() > 3)
            .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));

        // 方法3: 查找特定值
        System.out.println("\nEntries containing 'a':");
        map.entrySet().stream()
            .filter(entry -> entry.getValue().contains("a"))
            .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));

        db.close();
    }
}